package com.guigu.community.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.community.pojo.Merchants;
import com.guigu.community.service.MerchantsService;
import com.guigu.community.vo.MerchantsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/merchant")
public class MerchantsController {

    @Autowired
    private MerchantsService merchantsService;

    @PostMapping("/AddMerchant/{uid}")
    @ResponseBody
    public String AddSupplier(@RequestBody Merchants merchants, @PathVariable Integer uid){
        merchants.setIsBusiness(0);
        merchants.setUserId(uid);
        merchants.setBalance(0);
        merchants.setStatus(0);
        boolean save = merchantsService.save(merchants);
        if (save)
            return "yes";
        return "NO";
    }

    @PostMapping("/MerchantsAll/{pageNo}/{pageSize}")
    @ResponseBody
    public PageInfo<Merchants> MerchantsAll(@PathVariable Integer pageNo,
                                            @PathVariable Integer pageSize,
                                            @RequestBody MerchantsVo vo){
        return merchantsService.queryMerchants(pageNo, pageSize, vo);
    }

//    @PutMapping("/MerchantsUpdate")
//    @ResponseBody
//    public String MerchantsUpdate (@RequestBody Merchants merchants){
//        boolean b = merchantsService.updateById(merchants);
//        if (b)
//            return "yes";
//        return "no";
//    }

    // 审核通过接口
    @PostMapping("/approveAndUpdateUser/{merchantId}")
    @ResponseBody
    public boolean approveMerchant(@PathVariable Integer merchantId) {
        return merchantsService.approveMerchant(merchantId);
    }

    // 审核拒绝接口
    @PostMapping("/reject/{merchantId}")
    @ResponseBody
    public boolean rejectMerchant(@PathVariable Integer merchantId) {
        return merchantsService.rejectMerchant(merchantId);
    }

    @PostMapping("/selectByStatusZero/{current}/{size}")
    @ResponseBody
    public PageInfo<Merchants> selectByStatusZero(@PathVariable Integer current,
                                                  @PathVariable Integer size,
                                                  @RequestBody MerchantsVo vo){
        return merchantsService.selectByVo(current, size, vo);
    }

    @PostMapping("/selectByVo/{current}/{size}")
    @ResponseBody
    public PageInfo<Merchants> selectByVo(@PathVariable Integer current,
                                                  @PathVariable Integer size,
                                                  @RequestBody MerchantsVo vo){
        return merchantsService.selectByVo2(current, size, vo);
    }

    // 根据商户 ID 查询商户信息
    // 添加 selectById 接口
    @GetMapping("/selectById/{merchantId}")
    @ResponseBody
    public Merchants selectById(@PathVariable Integer merchantId) {
        return merchantsService.getById(merchantId);
    }

    @PutMapping("/MerchantsUpdate")
    @ResponseBody
    public String MerchantsUpdate (@RequestBody Merchants merchants){
        boolean b = merchantsService.updateById(merchants);
        if (b)
            return "yes";
        return "no";
    }

    @DeleteMapping("/deleteById/{merchantId}")
    @ResponseBody
    public boolean deleteById(@PathVariable Integer merchantId) {
        return merchantsService.removeById(merchantId);
    }
}